package eerbaar.client.internal.ctr.operator;

import eerbaar.client.ctr.OperatorConstants;
import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IScope;
import eerbaar.client.internal.ctr.operator.area.ValueArea;
import eerbaar.client.internal.ctr.operator.area.ValueAreaPopulator;
import eerbaar.elements.IElement;
import eerbaar.game.IGame;
import eerbaar.position.IPosition;

class Attract extends AbstractExp {

    public Attract(IExp[] array) {
        super(array);
    }

    @Override
    public void populateValueArea(IScope scope) {

        IGame game = (IGame) scope.getGame();
        ValueArea va = (ValueArea) scope.getValueArea();
        if (va == null) {
            va = new ValueArea(game);
            scope.setValueArea(va);
        }
        IExp[] exps = new IExp[] { this };
        ValueAreaPopulator populator = new ValueAreaPopulator(va, game, exps);
        populator.populateValueArea(scope);
    }

    @Override
    public String toString() {

        return "" + super.getElementName() + OperatorConstants.ATTRACT;
    }

    @Override
    public void updateArea(IScope scope) {

        if (super.getElementName() == null) {
            return;
        }

        IGame game = (IGame) scope.getGame();
        ValueArea area = (ValueArea) scope.getValueArea();
        double count = 0;

        int magnitude = super.getNumeric();
        int pattern = super.getPatternNumer();

        IElement[] elements = game.getArea().getElements();
        for (IElement e : elements) {

            if (e.getName().equals(super.getElementName())) {

                IPosition p = e.getPosition();
                area.incrementArea(p.getX(), p.getY(), 12, magnitude, pattern);
                count++;
            }
        }
    }

}
